<?php

namespace Xin\Robot\Laravel;

use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\ServiceProvider;
use Xin\Robot\Contracts\Factory as RobotFactory;
use Xin\Robot\RobotManager;

class RobotServiceProvider extends ServiceProvider
{
	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register()
	{
		$this->app->singleton(RobotManager::class, function ($app) {
			return new RobotManager(config('robot') ?: []);
		});
		$this->app->alias(RobotManager::class, 'robot');
		$this->app->alias(RobotManager::class, RobotFactory::class);

		$this->app->singleton('robot.default', function ($app) {
			return $app['robot']->robot();
		});
	}

	/**
	 * Register the config for publishing
	 *
	 */
	public function boot()
	{
		if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
			$this->publishes([$this->configPath() => config_path('robot.php')], 'robot');
		} elseif ($this->app instanceof LumenApplication) {
			$this->app->configure('robot');
		}
	}

	/**
	 * Set the config path
	 *
	 * @return string
	 */
	protected function configPath()
	{
		return __DIR__ . '/../../config/config.php';
	}
}
